iT邦幫忙

0

30天 Leetcode挑戰_Day 3

  • 分享至 

  • xImage
  •  

之前資料結構寫過一模一樣的題目回憶起來蠻快的

題目敘述:
94. Binary Tree Inorder Traversal
Given the root of a binary tree, return the inorder traversal of its nodes' values.

/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode() : val(0), left(nullptr), right(nullptr) {}
*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
   vector<int> num;
   vector<int> inorderTraversal(TreeNode* root) {
       if (root==nullptr){
           return num;//case樹空
       }
       inorderTraversal(root->left);//用遞迴從左子開始
       num.push_back(root->val);
       inorderTraversal(root->right); 
       return num;      
   }
};

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言